import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/adm/HomeView.vue'
import { ElMessage } from "element-plus";

const routes = [
  {
    path: '/',
    redirect: '/login',

  },
  {path: '/adminHome',
    name: 'home',
    component: HomeView,
    redirect: '/user',
    children: [
      { path: '/user', component: () => import('@/views/adm/adm_user/UserView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/client', component: () => import('@/views/adm/adm_user/ClientView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/branch', component: () => import('@/views/adm/adm_branch/BranchView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/branchMap', component: () => import('@/views/adm/adm_branch/BranchMapView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/dishType', component: () => import('@/views/adm/adm_dish/DishTypeView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/dish', component: () => import('@/views/adm/adm_dish/DishView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/orders', component: () => import('@/views/adm/adm_orders/OrdersView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/orderDetail', component: () => import('@/views/adm/adm_orders/OrderDetailView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/reservation', component: () => import('@/views/adm/adm_schedule/ReservationView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/tables', component: () => import('@/views/adm/adm_tables/TablesView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/statementCount', component: () => import('@/views/adm/adm_statement/StatementCountView.vue'), meta: { requiresAuth: true, roles: ['0'] } },
      { path: '/statementAmount', component: () => import('@/views/adm/adm_statement/StatementAmountView.vue'), meta: { requiresAuth: true, roles: ['0'] } }
    ]},
  // 顾客页面
  {
    path: '/userHome',
    name: 'userHome',
    component: () => import('../views/customer/CustomerHome.vue'),
    redirect: 'index',
    meta: { requiresAuth: true, roles: ['1'] },
    children: [
      { path: '/index', component: () => import('@/views/customer/Index.vue') },
      { path: '/myShoppingCart', component: () => import('@/views/customer/MyShoppingCart.vue'), meta: { requiresAuth: true, roles: ['1'] } },
      { path: '/myComment', component: () => import('@/views/customer/MyComment.vue'), meta: { requiresAuth: true, roles: ['1'] } },
      { path: '/myOrders', component: () => import('@/views/customer/MyOrders.vue'), meta: { requiresAuth: true, roles: ['1'] } },
      { path: '/myMessage', component: () => import('@/views/customer/MyMessage.vue'), meta: { requiresAuth: true, roles: ['1'] } },
      { path: '/myChat', component: () => import('@/views/customer/SendAndReceiveMessage.vue'), meta: { requiresAuth: true, roles: ['1'] } },

      {
        path: '/meal',
        component: () => import('@/views/customer/Meal.vue'),
        redirect: 'dishCard',
        meta: { requiresAuth: true, roles: ['1'] },
        children: [
          { path: '/userComment', component: () => import('@/views/use/use_comment/CommentView.vue'), meta: { requiresAuth: true, roles: ['1'] } },
          { path: '/dishCard', component: () => import('@/views/customer/DishCard.vue'), meta: { requiresAuth: true, roles: ['1'] } },
          { path: '/myTable', component: () => import('@/views/customer/MyTable.vue'), meta: { requiresAuth: true, roles: ['1'] } },
          { path: '/userAffirmCome', component: () => import('@/views/use/use_schedule/AffirmComeView.vue'), meta: { requiresAuth: true, roles: ['1'] } },
          { path: '/userReservation', component: () => import('@/views/use/use_schedule/UserReservationView.vue'), meta: { requiresAuth: true, roles: ['1'] } }
        ]
      },
      { path: '/myself', component: () => import('@/views/use/use_myself/MyselfView.vue'), meta: { requiresAuth: true, roles: ['1'] } }
    ]
  },
  // 员工页面
  {
    path: '/empHome',
    name: 'empHome',
    redirect: '/tablesCard',
    component: () => import('../views/emp/EmpHomeView.vue'),
    meta: { requiresAuth: true, roles: ['2'] },
    children: [
      { path: '/audit', component: () => import('@/views/emp/emp_schedule/AuditView.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/distribute', component: () => import('@/views/emp/emp_schedule/DistributeView.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/comment', component: () => import('@/views/emp/emp_comment/CommentView.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/tablesCard', component: () => import('@/views/emp/emp_tables/TablesCard.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/leaveTables', component: () => import('@/views/emp/emp_tables/LeaveTablesCard.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/handleOrders', component: () => import('@/views/emp/emp_orders/EmpOrderHandleView.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/empOrderDetail', component: () => import('@/views/emp/emp_orders/EmpOrdersDetailView.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/empSelectTable', component: () => import('@/views/emp/emp_ordered/SelectTable.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/empDishCard', component: () => import('@/views/emp/emp_ordered/DishCard.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/empShoppingCart', component: () => import('@/views/emp/emp_ordered/EmpShoppingCart.vue'), meta: { requiresAuth: true, roles: ['2'] } },
      { path: '/empChat', component: () => import('@/views/emp/emp_chat/SendAndReceiveMessage.vue'), meta: { requiresAuth: true, roles: ['2'] } }
    ]
  },
  // 通用登录页面
  { path: '/login', component: () => import('../views/LoginView') },
  // 用户注册页面
  { path: '/singLn', component: () => import('../views/SingInView.vue') }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局前置守卫：登录拦截 本地没有存用户数据,请重新登录
router.beforeEach((to, from, next) => {
  const user = getUser();
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!user.username) {
      ElMessage.info('请先登录');
      next({ path: '/login' });
    } else {
      // 检查用户角色是否匹配
      const allowedRoles = to.matched.map(route => route.meta.roles).flat().filter(role => role);
      if (!allowedRoles.includes(user.role)) {
        ElMessage.error('您没有权限访问该页面');
        next({ path: '/' }); // 或者重定向到其他页面
      } else {
        next();
      }
    }
  } else {
    next();
  }
});

function getUser() {
  const userStr = localStorage.getItem('user');
  return userStr ? JSON.parse(userStr) : { username: '', role: '' };
}

export default router



